#include "StdAfx.h"
#include "PlayerPack.h"
#include "Road.h"
#include "House.h"
#include "Edge.h"
#include "BuildPoint.h"

namespace catan
{
	PlayerPack::PlayerPack(void)
	{
	}
	
	PlayerPack::~PlayerPack(void)
	{
	}

	void PlayerPack::SetPlayerColor( PlayerColor::Enum color )
	{
		mRoads.SetColor(color);
		mHouses.SetColor(color);
		mSettlements.SetColor(color);
	}

	struct RoadFinder
	{
		RoadFinder(Roads& roads)
		{
			int maxLength = 0;
			for (std::vector<Handle<Road>>::iterator it(roads.GetVector().begin());
				it != roads.GetVector().end(); ++it)
			{
				if (!(*it)->HasBuilt() )
				{
					continue;
				}
				
				Handle<Road> curRoad = *it;
				int ret = FindMaxLenth((*it)->GetEdge());
				if (ret > mMaxLength)
				{
					mMaxLength = ret;
				}
			}
		}

		int FindMaxLenth(Handle<Edge> edge)
		{
			if (mEdgeFound.find(edge) == mEdgeFound.end())
			{
				return 0;
			}

			if (!edge->mRoad)
			{
				return 0;
			}
			else if (edge->mRoad->GetColor() != mColor)
			{
				return 0;
			}
			int ret = 0;
			mEdgeFound.insert(edge);
			for (BuildPoints::iterator it(edge->mBuildPoints.begin());
				it != edge->mBuildPoints.end(); ++it)
			{
				for (Edges::iterator eIt((*it)->mEdges.begin());
					eIt != (*it)->mEdges.end(); ++eIt)
				{
					ret += FindMaxLenth(*eIt);
				}
			}
			return 0;
		}
		int GetMaxLength() const { return mMaxLength; }
	private:
		int mMaxLength;
		PlayerColor::Enum mColor;
		std::set<Handle<Edge>> mEdgeFound;
	};
	int PlayerPack::FindLongestRoadNum()
	{
		RoadFinder finder(mRoads);
		return finder.GetMaxLength();

	}
}	